package com.miku.blog.filter;

import com.miku.blog.domain.dto.ResourceURIDTO;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.utils.PathUtils;
import com.miku.blog.utils.RedisCache;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 判断当前访问路径是否支持匿名访问
 *
 *
 *
 * @author shkstart
 * @create ${}YEAR-06-15 21:26
 */

public class ResourceAuthoritiesFilter extends OncePerRequestFilter {

    private RedisCache redisCache;

    public ResourceAuthoritiesFilter() {
    }

    public ResourceAuthoritiesFilter(RedisCache redisCache) {
        this.redisCache = redisCache;
    }


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String requestURI = request.getRequestURI();
        if (SystemConstance.SWAGGER_URI.contains(requestURI)) {
            request.setAttribute(SystemConstance.REQUEST_ATTRIBUTE_RESOURCE_ANONYMOUS,
                    SystemConstance.IS_ANONYMOUS);
            //放行Swagger Api文档请求路径
            filterChain.doFilter(request, response);
            return;
        }

        String prefix = PathUtils.getPathPrefix(requestURI);

        List<ResourceURIDTO> resources
                = redisCache.getCacheHashMapValue(SystemConstance.RESOURCE_REQUEST_URI_LIST_KEY, prefix);


        if (requestURI.equals(prefix)){

            //如果requestURI与裁剪后的 路径 一致
            //则直接查找与之一样的路径 与 请求方法

            for (ResourceURIDTO dto : resources) {

                if (requestURI.equals(dto.getUrl()) && request.getMethod().equalsIgnoreCase(dto.getRequestMethod())){
                    request.setAttribute(SystemConstance.REQUEST_ATTRIBUTE_RESOURCE_ANONYMOUS,
                            dto.getIsAnonymous());
                    break;
                }
            }

        }else {

            //如果requestURI与裁剪后的 路径 不一致,则判断是否存在与之一样的路径 与 请求方法
            //若不存在，则判断是否为RESTful风格

            //记录 requestURI 是否 与 某一非Restful风格的路径相同,若存在 则跳过判断Restful风格路径
            boolean sameURI = true;

            one:for (ResourceURIDTO dto : resources) {

                String url = dto.getUrl();
                String method = dto.getRequestMethod();

                if (requestURI.equals(url)){
                    sameURI = false;
                    if (request.getMethod().equalsIgnoreCase(method)){
                        request.setAttribute(SystemConstance.REQUEST_ATTRIBUTE_RESOURCE_ANONYMOUS,
                                dto.getIsAnonymous());
                        break;
                    }
                }else if (sameURI){
                    String[] requestURISplit = requestURI.split("/");
                    String[] urlSplit = url.split("/");
                    if (requestURISplit.length == urlSplit.length){

                        for (int i = 1;i < urlSplit.length;i++){
                            String var1 = requestURISplit[i];
                            String var12 = urlSplit[i];
                            if (!var1.equals(var12) && !var12.equals(SystemConstance.RESTFUL)){
                                continue one;
                            }
                        }
                        if (request.getMethod().equalsIgnoreCase(method)) {
                            request.setAttribute(SystemConstance.REQUEST_ATTRIBUTE_RESOURCE_ANONYMOUS,
                                    dto.getIsAnonymous());
                            break;
                        }
                    }
                }

            }

        }

        filterChain.doFilter(request,response);
        request.removeAttribute(SystemConstance.REQUEST_ATTRIBUTE_RESOURCE_ANONYMOUS);
    }









}
